define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tongcheng/module_config/index' + location.search,
                    add_url: 'tongcheng/module_config/add',
                    edit_url: 'tongcheng/module_config/edit',
                    del_url: 'tongcheng/module_config/del',
                    multi_url: 'tongcheng/module_config/multi',
                    import_url: 'tongcheng/module_config/import',
                    table: 'tongcheng_module_config',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'module_id', title: __('Module_id')},
                        {field: 'base_fee', title: __('Base_fee'), operate:'BETWEEN'},
                        {field: 'open_tip', title: __('Open_tip'), searchList: {"1":__('Open_tip 1'),"0":__('Open_tip 0')}, formatter: Table.api.formatter.normal},
                        {field: 'open_advance_fee', title: __('Open_advance_fee'), searchList: {"1":__('Open_advance_fee 1'),"0":__('Open_advance_fee 0')}, formatter: Table.api.formatter.normal},
                        {field: 'open_insure', title: __('Open_insure'), searchList: {"1":__('Open_insure 1'),"0":__('Open_insure 0')}, formatter: Table.api.formatter.normal},
                        {field: 'open_range', title: __('Open_range'), searchList: {"1":__('Open_range 1'),"0":__('Open_range 0')}, formatter: Table.api.formatter.normal},
                        {field: 'open_weight', title: __('Open_weight'), searchList: {"1":__('Open_weight 1'),"0":__('Open_weight 0')}, formatter: Table.api.formatter.normal},
                        {field: 'open_surcharge', title: __('Open_surcharge'), searchList: {"1":__('Open_surcharge 1'),"0":__('Open_surcharge 0')}, formatter: Table.api.formatter.normal},
                        {field: 'open_appoint_time', title: __('Open_appoint_time'), searchList: {"1":__('Open_appoint_time 1'),"0":__('Open_appoint_time 0')}, formatter: Table.api.formatter.normal},
                        {field: 'open_gender', title: __('Open_gender'), searchList: {"1":__('Open_gender 1'),"0":__('Open_gender 0')}, formatter: Table.api.formatter.normal},
                        {field: 'open_coupon', title: __('Open_coupon'), searchList: {"1":__('Open_coupon 1'),"0":__('Open_coupon 0')}, formatter: Table.api.formatter.normal},
                        {field: 'open_pickup_code', title: __('Open_pickup_code'), searchList: {"1":__('Open_pickup_code 1'),"0":__('Open_pickup_code 0')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'module.name', title: __('Module.name'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        range: function () {
            Controller.api.bindevent();
        },
        weight: function () {
            Controller.api.bindevent();
        },
        surcharge: function () {
            // Controller.api.bindevent();
            // if (!_controller) {
            //   _controller = Controller;
            // }
            // if (!_form) {
            //   _form = Form;
            // }

            // $(document).on("fa.event.appendfieldlist", '[data-name="row[surcharge_config]"] .btn-append', function(e, obj){
            //     //通用的表单组件事件绑定和组件渲染
            //     Form.api.bindevent(obj);
            // });

            Form.api.bindevent($("form[role=form]"), function(data, ret){
              // success
            }, function(data, ret){
              // error
            }, function (success, error) {
              // before submit
              console.log('valid', app);
              // console.log('ret', ret);
              // if (ret.code == 1) {
              //   Fast.api.close();
              // }

              valid_result = app.valid();

              return valid_result;
            });
        },
        overtime: function () {
            Form.api.bindevent($("form[role=form]"), function(data, ret){
              // success
            }, function(data, ret){
              // error
            }, function (success, error) {
              // before submit
              console.log('valid', app);
              // console.log('ret', ret);
              // if (ret.code == 1) {
              //   Fast.api.close();
              // }

              valid_result = app.valid();

              return valid_result;
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
